#include"UserInfoDAO.h"
#include"DBConnPool.h"
#include"Logger.h"
using namespace Log;

bool UserInfoDAO::vertify(const std::string& username, const std::string& password, std::string& result, DBConnPtr m_SQLCON) {
    if (!m_SQLCON)
        m_SQLCON = DBConnPool::getInstance()->getDBConn();
    std::string sql("SELECT username, password FROM userInfo WHERE username = \"" + username + "\" AND password = \"" +
        password + "\";");
    if (mysql_query(m_SQLCON.get(), sql.c_str())) {
        result.clear();
        result += std::string(mysql_error(m_SQLCON.get()));
        LOG_DEBUG << result;
        return false;
    }
    auto res = mysql_store_result(m_SQLCON.get());
    int num_fields = mysql_num_fields(res);
    if (num_fields <= 0) {
        result = std::string("Query error\n");
        return false;
    }

    MYSQL_ROW row = mysql_fetch_row(res);
    if (!row) {
        result = std::string("username: " + username + " does not exist!");
        return false;
    }
    auto username_ = std::string(row[0]);
    auto password_ = std::string(row[1]);
    if (username != username_) {
        result = std::string("username: " + username + " does not exist!");
        return false;
    }
    if (password_ != password) {
        result = std::string("password error!");
        return false;
    }
    return true;
}
bool UserInfoDAO::findUser(const std::string& username, std::string& result, DBConnPtr m_SQLCON) {
    if (!m_SQLCON)
        m_SQLCON = DBConnPool::getInstance()->getDBConn();
    std::string sql("SELECT username, password FROM userInfo WHERE username = \"" + username + "\";");
    if (mysql_query(m_SQLCON.get(), sql.c_str())) {
        result.clear();
        result += std::string(mysql_error(m_SQLCON.get()));
        LOG_DEBUG << result;
        return false;
    }
    auto res = mysql_store_result(m_SQLCON.get());
    int num_fields = mysql_num_fields(res);
    if (num_fields <= 0) {
        result = std::string("Query error\n");
        return false;
    }

    MYSQL_ROW row = mysql_fetch_row(res);
    if (!row) {
        result = std::string("username: " + username + " does not exist!");
        return false;
    }
    auto username_ = std::string(row[0]);
    auto password_ = std::string(row[1]);
    if (username != username_) {
        result = std::string("username: " + username + " does not exist!");
        return false;
    }
    return true;
}

bool UserInfoDAO::registUser(const std::string& username, const std::string& password, std::string& result, DBConnPtr m_SQLCON) {
    if (!m_SQLCON)
        m_SQLCON = DBConnPool::getInstance()->getDBConn();
    //std::string tmp;
    //if (findUser(username, tmp, m_SQLCON)) {
    //    result = std::string("username: " + username + " does exist!");
    //    return false;
    //}
    std::string sql(
        "INSERT INTO userInfo(username, password) " + std::string("VALUES (\"") + username + "\", \"" + password + "\");");
    if (mysql_query(m_SQLCON.get(), sql.c_str())) {
        result = std::string(mysql_error(m_SQLCON.get()));
        LOG_DEBUG << result;
        return false;
    }
    return true;
}